package com.foreverwin.jzymes.production.activity.controller;

import com.foreverwin.jzymes.common.service.IActivityRuleAdviceService;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
    @RequestMapping("/activityRule")
public class ActivityRuleController {

    @Autowired
    IActivityRuleAdviceService activityRuleAdviceService;

    @RequestMapping("/findActivityRule")
    public AjaxResult findActivityRule( String activityId, String ruleName ){
        Map<String,String> activityRuleMap = activityRuleAdviceService.findActivityRules( activityId );
        String[] ruleNames = ruleName.split( "," );
        List<Map<String,String>> rtnRuleList = new ArrayList<>();
        for( String _ruleName : ruleNames ){
            if( activityRuleMap.containsKey( _ruleName ) ){
                Map<String,String> rtnRuleMap = new HashMap<>();
                rtnRuleMap.put( "ruleName", _ruleName );
                rtnRuleMap.put( "value", activityRuleMap.get( _ruleName ) );
                rtnRuleList.add( rtnRuleMap );
            }
        }
        return AjaxResult.returnSuccess().put( "value", rtnRuleList );
    }

    @RequestMapping("/saveActivityRule")
    @ParamBind
    public AjaxResult saveActivityRule( String activityId, ArrayList<Map<String,Object>> ruleList ){
        try{
            activityRuleAdviceService.updateActivityRule( activityId, ruleList );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }
}
